home *** CD-ROM | disk | FTP | other *** search
/ CDUTIL 13 / CDUTIL #13 Julio 1995.iso / windows / acadcom / acrx / sample / grvecs.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  12.9 KB  |  406 lines

  1. /* Next available MSG number is  33 */
  2.  
  3. /***************************************************************************
  4.    Module Name:  grvecs.cc
  5.  
  6.    Copyright (C) 1994 by Autodesk, Inc.
  7.  
  8.    Permission to use, copy, modify, and distribute this software in 
  9.    object code form for any purpose and without fee is hereby granted, 
  10.    provided that the above copyright notice appears in all copies and 
  11.    that both that copyright notice and the limited warranty and 
  12.    restricted rights notice below appear in all supporting 
  13.    documentation.
  14.  
  15.    AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.  
  16.    AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF 
  17.    MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC.
  18.    DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE 
  19.    UNINTERRUPTED OR ERROR FREE.
  20.  
  21.    Use, duplication, or disclosure by the U.S. Government is subject to 
  22.    restrictions set forth in FAR 52.227-19 (Commercial Computer 
  23.    Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) 
  24.    (Rights in Technical Data and Computer Software), as applicable.
  25.     
  26.    .
  27.  
  28.    Description:RXADS version of the AutLISP program sprial.lsp
  29.  
  30.    Author     :
  31.                  Autodesk, Inc.
  32.                  2320 Marinship Way
  33.                  Sausalito, CA. 94965
  34.                  (415)332-2344
  35.  
  36.     This Arx application is a conversion from the original sample ADS app
  37.     grvecs.c.
  38.  
  39.     CREATED BY:  William Howison, January 1994
  40.  
  41.     What it tests :
  42.  
  43.     - ads_grvecs function.
  44.  
  45.     Function Entry Points:
  46.       AcRx::AppRetCode
  47.         acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt);
  48.  
  49.     Exported ADS Functions
  50.         TESTGRVECS                [Test the ADS_GRVECS function]
  51.  
  52.     Modification History:
  53.         Jan 31 1994 - bch - original creation
  54.  
  55.     Notes and restrictions on use:
  56.  
  57.  
  58. ***************************************************************************/
  59.  
  60. /**************************************************************************/
  61. /*  MODULE NAME  */
  62. /**************************************************************************/
  63. #define    GRVECS
  64.  
  65. /****************************************************************************/
  66. /*  DEFINES  */
  67. /****************************************************************************/
  68. #define ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
  69. #define SPOINT(p, x, y, z) p[X] = (x); p[Y] = (y); p[Z] = (z)
  70. #define CPOINT(dst, src) dst[X] = src[X]; dst[Y] = src[Y]; dst[Z] = src[Z]
  71. /**************************************************************************/
  72. /*  TYPEDEFS  */
  73. /**************************************************************************/
  74. /* ADS Function Table */
  75. typedef struct {
  76.     char    *name;
  77.     int     (*fptr)();
  78. } ftblent;
  79.  
  80. typedef struct resbuf rbtype;
  81. /**************************************************************************/
  82. /*  INCLUDES  */
  83. /**************************************************************************/
  84.  
  85. #include <math.h>
  86. #include <memory.h>
  87. #include <stdio.h>
  88. #include "adslib.h"
  89. #include "rxdefs.h"
  90. #include "adesk.h"
  91. #include "ol_errno.h"
  92.  
  93. extern "C" {
  94. /****************************************************************************/
  95. /*  LOCALLY DEFINED ENTRY POINT INVOKED BY Arx                              */
  96. /****************************************************************************/
  97. AcRx::AppRetCode acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt);
  98. }
  99.  
  100. /****************************************************************************/
  101. /*  LOCAL FUNCTION FORWARD DECLARATIONS  */
  102. /****************************************************************************/
  103. int grspiral   _((int ntimes, ads_point bpoint, ads_real cfac, int lppass));
  104. int testgrvecs _((void));
  105.  
  106. /**************************************************************************/
  107. /*  GLOBAL VARIABLES  */
  108. /**************************************************************************/
  109. /* Table of ADS functions */
  110. ftblent exfun[] = {
  111.             {/*MSG0*/"C:TESTGRVECS", testgrvecs},
  112.         };
  113.  
  114. ads_matrix ads_identmat = {
  115.     {1.0, 0.0, 0.0, 0.0},
  116.     {0.0, 1.0, 0.0, 0.0},
  117.     {0.0, 0.0, 1.0, 0.0},
  118.     {0.0, 0.0, 0.0, 1.0}
  119. };
  120.  
  121. /**************************************************************************/
  122. /*  EXTERNAL FUNCTION DECLARATIONS  */
  123. /**************************************************************************/
  124.  
  125. /**************************************************************************/
  126. /*  EXTERNAL VARIABLE DECLARATIONS  */
  127. /**************************************************************************/
  128.  
  129. /****************************************************************************/
  130. /*  LOCAL FUNCTION DECLARATIONS  */
  131. /****************************************************************************/
  132. int geterrno   _((void));
  133. int funcload   _((void));
  134. int funcunload _((void));
  135. int dofun      _((void));
  136.  
  137. /******************************************************************************/
  138. /*.doc geterrno(internal) */
  139. /*+
  140.     This function is called to obtain the value of the AutoCAD system
  141.     variable ERRNO and return it as a result.
  142. -*/
  143. /******************************************************************************/
  144. int
  145. /*FCN*/geterrno()
  146. {
  147.     rbtype errval;
  148.  
  149.     ads_getvar(/*MSG0*/"ERRNO", &errval);
  150.  
  151.     return errval.resval.rint;
  152. }
  153.  
  154. /******************************************************************************/
  155. /*.doc funcload(internal) */
  156. /*+
  157.     This function is called to define all function names in the ADS
  158.     function table.  Each named function will be callable from lisp or
  159.     invokable from another ADS application.
  160. -*/
  161. /******************************************************************************/
  162. int
  163. /*FCN*/funcload()
  164. {
  165.     int i;
  166.  
  167.     for (i = 0; i < ELEMENTS(exfun); i++) {
  168.         if (!ads_defun(exfun[i].name, i))
  169.             return RTERROR;
  170.     }
  171.  
  172.     return RTNORM;
  173. }
  174.  
  175. /******************************************************************************/
  176. /*.doc funclunoad(internal) */
  177. /*+
  178.     This function is called to undefine all function names in the ADS
  179.     function table.  Each named function will be removed from the
  180.     AutoLISP hash table.
  181. -*/
  182. /******************************************************************************/
  183. int
  184. /*FCN*/funcunload()
  185. {
  186.     int i;
  187.  
  188.     /* Undefine each function we defined */
  189.  
  190.     for (i = 0; i < ELEMENTS(exfun); i++) {
  191.         ads_undef(exfun[i].name,i);
  192.     }
  193.  
  194.     return RTNORM;
  195. }
  196. /******************************************************************************/
  197. /*.doc dofun(internal) */
  198. /*+
  199.     This function is called to invoke the function which has the
  200.     registerd function code that is obtained from  ads_getfuncode.  The
  201.     function will return RTERROR if the function code is invalid, or
  202.     RSERR if the invoked function fails to return RTNORM.  The value
  203.     RSRSLT will be returned if the function code is valid and the
  204.     invoked subroutine returns RTNORM.
  205. -*/
  206. /******************************************************************************/
  207. int
  208. /*FCN*/dofun()
  209. {
  210.     int    val;
  211.     int    rc;
  212.  
  213.     ads_retvoid();
  214.  
  215.     if ((val = ads_getfuncode()) < 0 || val > ELEMENTS(exfun))
  216.         return RTERROR;
  217.  
  218.     rc = (*exfun[val].fptr)();
  219.  
  220.     return ((rc == RTNORM) ? RSRSLT:RSERR);
  221. }
  222.  
  223.  
  224. /******************************************************************************/
  225. /*.doc grspiral(internal) */
  226. /*+
  227.     This function is a C translation of the grspiral.lsp program
  228.     designed to exersise the AutoLISP function grvecs.  The function
  229.     takes the arguments: number of spiral rotations, the center point
  230.     for the spiral, the rate of growth per rotation, and the number of
  231.     points per rotation.
  232.  
  233.     The vectors for the spiral will be generated in the form of a
  234.     linked list of three dimentional point nodes, and color short
  235.     nodes.  The resulting list will be passed to the ads_grvecs
  236.     function which will perform the actual graphical output.  Each
  237.     vector in the list will include a color value which is obtained by
  238.     cycling thru the range of colors specified by the values of minclr,
  239.     and maxclr.
  240.  
  241.     This function always returns RTNORM.
  242. -*/
  243. /******************************************************************************/
  244. int
  245. /*FCN*/grspiral(int ntimes, ads_point bpoint, ads_real cfac, int lppass)
  246. {
  247.     ads_real     circle;
  248.     ads_real     ainc;
  249.     ads_real     dinc;
  250.     ads_real     ang;
  251.     ads_real     dist;
  252.     ads_real     zdir;
  253.     ads_point    lpoint;
  254.     ads_point    tp;
  255.     rbtype       *plist;
  256.     rbtype       *tplist;
  257.     int          minclr;
  258.     int          maxclr;
  259.     int          color;
  260.     int          n;
  261.     int          l;
  262.     int          sindex;
  263.     ads_matrix   mat;
  264.  
  265.     memcpy(mat, ads_identmat, sizeof(ads_matrix));
  266.     CPOINT(lpoint, bpoint);
  267.     circle = 3.141496235 * 2;
  268.     ainc = circle / lppass;
  269.     dinc = cfac / lppass;
  270.     ang = 0.0;
  271.     dist = 0.0;
  272.     minclr = 1;
  273.     maxclr = 200;
  274.     color = minclr;
  275.     zdir = 0.0;
  276.  
  277.     ads_retnil();
  278.     tplist = plist = ads_buildlist(RTSHORT, color, NULL);
  279.  
  280.     for (n = ntimes; n > 0 && tplist != NULL; n--) {
  281.         for (l = lppass; l > 0 && tplist != NULL; l--) {
  282.             ang = ang + ainc;
  283.             dist = dist + dinc;
  284.             ads_polar(bpoint, ang, dist, tp);
  285.  
  286.             /* adjust z coordinate to this point */
  287.             tp[Z] = zdir;
  288.  
  289.             /* append last point and new point to end of grvecs list */
  290.             tplist = tplist->rbnext = ads_buildlist(RT3DPOINT, lpoint, NULL);
  291.             if (tplist == NULL)
  292.                 continue;
  293.             tplist = tplist->rbnext = ads_buildlist(RT3DPOINT, tp, NULL);
  294.             if (tplist == NULL)
  295.                 continue;
  296.  
  297.             /* increment color */
  298.             color++;
  299.  
  300.             /* if color is == max color then reset it to one and
  301.                loop thru them again */
  302.             if (color == maxclr)
  303.                 color = minclr;
  304.  
  305.             /* append new color number to end of grvecs list */
  306.             tplist = tplist->rbnext = ads_buildlist(RTSHORT, color, NULL);
  307.             if (tplist == NULL)
  308.                 continue;
  309.  
  310.             /* save new point as last point */
  311.             CPOINT(lpoint, tp);
  312.  
  313.             /* adjust the z coord for the next point */
  314.             zdir = zdir + cfac;
  315.         }
  316.     }
  317.     if (plist != NULL && tplist != NULL)
  318.         ads_grvecs(plist, NULL);
  319.         for (sindex = 0; sindex < 100; sindex ++) {
  320.             mat[X][X] = mat[Y][Y] = mat[Z][Z] += .05;
  321.             ads_grvecs(plist, mat);
  322.         }
  323.     if (plist != NULL)
  324.         ads_relrb(plist);
  325.     return RTNORM;
  326. }
  327.  
  328.  
  329. /******************************************************************************/
  330. /*.doc testgrvecs(internal) */
  331. /*+
  332.     This function is called from dofun function as a result of RQSUBR
  333.     request being sent to the main dispatch loop.
  334.  
  335.     It prompts the user for the Center point of a spiral, the number of
  336.     rotations in the spiral, the amount of growth per rotation, and the
  337.     number of points in each rotation.   The function can be canceled
  338.     by the user by pressing the Control-C keys.
  339.  
  340.     This function does not perfrom an error checking.  The return value
  341.     for this function is the last value in rc.
  342. -*/
  343. /******************************************************************************/
  344. int
  345. /*FCN*/testgrvecs()
  346. {
  347.     rbtype      *args;
  348.     int         rc;
  349.     ads_point   bp;
  350.     int         nt;
  351.     ads_real    cf;
  352.     int         lp;
  353.  
  354.     args = ads_getargs();
  355.     ads_printf(/*MSG3*/"\nPerforming ads_grvecs test\n");
  356.     if (args != NULL) {
  357.         ads_printf(/*MSG4*/"No arguments please.\n");
  358.     }
  359.     rc = ads_getpoint(NULL, /*MSG5*/"\nCenter point: ", bp);
  360.     if (rc == RTCAN) return RTNORM;
  361.     rc = ads_getint(/*MSG6*/"\nNumber of rotations: ", &nt);
  362.     if (rc == RTCAN) return RTNORM;
  363.     rc = ads_getreal(/*MSG7*/"\nGrowth per rotation: ", &cf);
  364.     if (rc == RTCAN) return RTNORM;
  365.     lp = 30;
  366.     ads_initget(0, NULL);
  367.     rc = ads_getint(/*MSG8*/"\nPoints per rotation <30>: ", &lp);
  368.     if (rc == RTCAN) return RTNORM;
  369.  
  370.     rc = grspiral(nt, bp, cf, lp);
  371.  
  372.     return rc;
  373. }
  374.  
  375. /* =================== Arx Module Interface Functions ================ */
  376.  
  377. AcRx::AppRetCode
  378. /*FCN*/acrxEntryPoint(AcRx::AppMsgCode msg, void * ptr)
  379. {
  380.  
  381.     if (ptr != NULL) {
  382.         // We have been handed some kind of object
  383.         // but we aren't going to do anything with it.
  384.     }
  385.  
  386.     switch(msg) {
  387.         case AcRx::kInitAppMsg:
  388.             break;
  389.         case AcRx::kInvkSubrMsg:
  390.             dofun();
  391.             break;
  392.         case AcRx::kLoadADSMsg:
  393.             funcload();
  394.             break;
  395.         case AcRx::kUnloadADSMsg:
  396.             funcunload();
  397.             ads_printf(/*MSG2*/"Unloading.\n");
  398.             break;
  399.         case AcRx::kUnloadAppMsg:
  400.         default:
  401.             break;
  402.     }
  403.     return AcRx::kRetOK;
  404. }
  405.  
  406.